{
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
}

unit QuestionEditors;

interface

uses
  Classes, SysUtils, VisualUtils, MiscUtils, Shape;

type
  TQuestionEditorRegistry = class
  private type
    TEntry = class
    private
      FQuestionEditorClass: TShapeFrameClass;
      FKind: TKind;
    public
      constructor Create(QuestionEditorClass: TShapeFrameClass; Kind: TKind);
    end;
    TEntryList = TGenericObjectList<TEntry>;
  private
    FEntries: TEntryList;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Add(QuestionEditorClass: TShapeFrameClass; Kind: TKind);
    function Find(Kind: TKind): TShapeFrameClass;
  end;

  {$INTERFACES CORBA}
  IQuestionEditor = interface
    ['71d944030589cea1']
    procedure Render;
  end;
  {$INTERFACES DEFAULT}

function QuestionEditorRegistry: TQuestionEditorRegistry;

implementation

var
  Registry: TQuestionEditorRegistry;

function QuestionEditorRegistry: TQuestionEditorRegistry;
begin
  Result := Registry;
end;

{ TQuestionEditorRegistry.TEntry }

constructor TQuestionEditorRegistry.TEntry.Create(
  QuestionEditorClass: TShapeFrameClass; Kind: TKind);
begin
  inherited Create;
  FQuestionEditorClass := QuestionEditorClass;
  FKind := Kind;
end;

{ TQuestionEditorRegistry }

constructor TQuestionEditorRegistry.Create;
begin
  inherited;
  FEntries := TEntryList.Create;
end;

destructor TQuestionEditorRegistry.Destroy;
begin
  FreeAndNil(FEntries);
  inherited;
end;

procedure TQuestionEditorRegistry.Add(QuestionEditorClass: TShapeFrameClass; Kind: TKind);
begin
  FEntries.AddSafely(TEntry.Create(QuestionEditorClass, Kind));
end;

function TQuestionEditorRegistry.Find(Kind: TKind): TShapeFrameClass;
var
  e: TEntry;
begin
  for e in FEntries do
    if e.FKind = Kind then
      Exit(e.FQuestionEditorClass);
  raise Exception.CreateFmt('Question editor %s not found.', [KindToString(Kind)]);
end;

initialization

  Registry := TQuestionEditorRegistry.Create;

finalization

  FreeAndNil(Registry);

end.

